/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.enrstu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.enrstu.domain.GuardianInformation;
import me.zhengjie.modules.enrstu.repository.GuardianInformationRepository;
import me.zhengjie.modules.enrstu.service.GuardianChildrenService;
import me.zhengjie.modules.enrstu.service.GuardianInformationService;
import me.zhengjie.modules.enrstu.service.dto.GuardianInformationDto;
import me.zhengjie.modules.enrstu.service.dto.GuardianInformationQueryCriteria;
import me.zhengjie.modules.enrstu.service.dto.SignUpGuardianInfoCreateOrUpdate;
import me.zhengjie.modules.enrstu.service.mapstruct.GuardianInformationMapper;
import me.zhengjie.utils.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyx
* @date 2024-11-01
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class GuardianInformationServiceImpl implements GuardianInformationService {

    private final GuardianInformationRepository guardianInformationRepository;
    private final GuardianInformationMapper guardianInformationMapper;



    @Override
    @Transactional
    public GuardianInformationDto findById(String id) {
        GuardianInformation guardianInformation = guardianInformationRepository.findById(id).orElseGet(GuardianInformation::new);
        ValidationUtil.isNull(guardianInformation.getId(),"GuardianInformation","id",id);
        return guardianInformationMapper.toDto(guardianInformation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GuardianInformation create(SignUpGuardianInfoCreateOrUpdate resources) {
        if (!ObjectUtil.isNull(resources.getGuardianIdentityCard())) {
            if (guardianInformationRepository.count((Root<GuardianInformation> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
                return  cb.equal(root.get("guardianIdentityCard"), resources.getGuardianIdentityCard());
            }) > 0) {
                throw new BadRequestException(resources.getGuardianName() + "监护人信息已存在！");
            }
        }
        GuardianInformation guardianInformation = new GuardianInformation();
        BeanUtils.copyProperties(resources,guardianInformation);
        guardianInformation.setId(IdUtil.simpleUUID());

        return guardianInformationRepository.save(guardianInformation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SignUpGuardianInfoCreateOrUpdate resources,Boolean isSubmit) {
        GuardianInformation guardianInformation = guardianInformationRepository.findById(resources.getId()).orElseGet(GuardianInformation::new);
        ValidationUtil.isNull( guardianInformation.getId(),"GuardianInformation","id",resources.getId());
        BeanUtils.copyProperties(resources,guardianInformation);
        GuardianInformation save = guardianInformationRepository.save(guardianInformation);
        if (isSubmit){
            this.submit(save);
        }
    }



    @Override
    public GuardianInformation findByCard(String idCard) {
        return guardianInformationRepository.findByGuardianIdentityCard(idCard);
    }

    private void submit(GuardianInformation request){
        Map<String, Object> map = BeanUtil.beanToMap(request, false, false);
            map.forEach((key, value)->{
                if (ObjectUtil.equal(key, "id")){
                    return;
                }
                if (ObjectUtil.isNull(value)){
                    throw  new BadRequestException("请确保所有必填项填写完成后提交");
                }
            });
    }
}